import { createRouter, createWebHistory } from 'vue-router';
import authentication from '@/utils/authentication';
// import notify from '@/api/notify';
// import { userStore } from '../stores/user';

const routes = [
    {
        path: '/',
        redirect: '/main/home'
    },

    {
        path: '/auth',
        name: 'auth',
        component: () => import('@/pages/auth/Auth.vue'),
        meta: {
            title: '登录注册',
            freeAuth: true // 免认证页面
        }
    },
    {
        path: '/main',
        component: () => import('@/components/main.vue'),
        meta: {
            requireAuth: true // 需要认证的页面
        },
        children: [
            {
                path: 'home',
                name: 'home',
                component: () => import('@/pages/user/home.vue'),
                meta: {
                    title: '主页',
                    requireAuth: true
                }
            },
            {
                path: 'history',
                name: 'history',
                component: () => import('@/pages/user/history.vue'),
                meta: {
                    title: '历史记录',
                    requireAuth: true
                }
            },
            {
                path: 'information',
                name: 'information',
                component: () => import('@/pages/user/information.vue'),
                meta: {
                    title: '个人信息',
                    requireAuth: true
                }
            },
            {
                path: 'uploadpicture',
                name: 'uploadpicture',
                component: () => import('@/pages/user/uploadpicture.vue'),
                meta: {
                    title: '上传图片',
                    requireAuth: true
                }
            },
            {
                path: 'detection-result',
                name: 'detection-result',
                component: () => import('@/pages/user/detection-result.vue'),
                meta: {
                    title: '检测结果',
                    requireAuth: true
                }
            }
            ,
            {
                path: 'canvas-annotate',
                name: 'canvas-annotate',
                component: () => import('@/pages/user/canvas-annotate.vue'),
                meta: {
                    title: '标注示例',
                    requireAuth: true
                }
            }

        ]
    },
    {
        // 捕获所有不存在的路径，重定向到主页
        path: '/:pathMatch(.*)*',
        redirect: '/main/home'
    }
];

const router = createRouter({
    history: createWebHistory(),
    routes
});

// 导航守卫 - 检查用户登录状态
router.beforeEach((to, from, next) => {
    // 使用authentication工具获取token
    const token = authentication.getToken();
    // 获取当前路由最后一条匹配记录的 meta 信息
    const currentRecord = to.matched[to.matched.length - 1];
    const freeAuthMeta = currentRecord?.meta?.freeAuth;
    const requireAuthMeta = currentRecord?.meta?.requireAuth;

    // 如果页面需要认证但没有 token，则跳转到登录页面
    if (requireAuthMeta && !token) {
        console.warn('访问需要认证的页面但未登录，跳转到登录页');
        return next({ name: 'auth' });
    }

    // 如果已登录用户访问登录页，重定向到主页
    if (token && to.name === 'auth') {
        return next({ name: 'home' });
    }

    // 通过验证，允许访问目标路由
    next();
});
export default router;
